package com.yc.star.booksystem.controller;

import com.yc.star.booksystem.constants.Constants;
import com.yc.star.booksystem.model.UserInfo;
import com.yc.star.booksystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 登录验证
    @RequestMapping("/login")
    public boolean login(String userName, String password, HttpSession session) {
        // 校验是否为空
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }

        UserInfo user = userService.getUserByName(userName);

        if (user == null) {
            return false;
        }

        // 校验用户名密码是否正确
        if (user.getPassword().equals(password)) {
            user.setPassword("");
            session.setAttribute(Constants.USER_SESSION_KEY,user);
            return true;
        }
        return false;
    }

}
